package com.wangsh.usersCenter.web.head.controller;

import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.wangsh.usersCenter.common.controller.BaseController;
import com.wangsh.usersCenter.common.pojo.ApiResponse;
import com.wangsh.usersCenter.common.pojo.ApiResponseEnum;
import com.wangsh.usersCenter.common.util.ConstatFinalUtil;
import com.wangsh.usersCenter.common.util.PageInfoUtil;
import com.wangsh.usersCenter.system.pojo.ASysPro;
import com.wangsh.usersCenter.system.pojo.ASysProEnum;
import com.wangsh.usersCenter.system.service.ISystemDbService;
import com.wangsh.usersCenter.users.pojo.AUsers;
import com.wangsh.usersCenter.users.pojo.AUsersApis;
import com.wangsh.usersCenter.users.service.IUsersDbService;
import com.wangsh.usersCenter.users.service.IUsersOperService;

/**
 * 	用户首页操作
 * 	@author wangsh
 */
@Controller
@RequestMapping("/head/users/")
public class UsersHeadController extends BaseController
{
	@Resource
	private IUsersOperService usersOperService;
	@Resource
	private IUsersDbService usersDbService;
	@Resource
	private ISystemDbService systemDbService;
	
	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main(HttpServletRequest request)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--main--");
		/* 查询所有导航网站 */
		/* 准备请求的信息 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 按照类型查询 */
		condMap.put("proType", ASysProEnum.PROTYPESTATUS_WEBSITEHEAD.getStatus());
		ApiResponse<ASysPro> apiResponse = this.systemDbService.findCondListSysProService(null, condMap);
		/* 清除信息 */
		apiResponse.clearInfo();
		request.setAttribute("response", apiResponse.toJSON());
		return "/head/users/main";
	}
	
	/**
	 * 欢迎页面
	 * @return
	 */
	@RequestMapping("/welcome")
	public String welcome()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--welcome--");
		return "/head/users/welcome";
	}
	
	/**
	 * 打开用户更新
	 * @return
	 */
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request,Model model)
	{
		
		ConstatFinalUtil.SYS_LOGGER.info("--usersUpdate--");
		
		String operType = request.getParameter("operType");
		/* 从session中获取信息 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 准备请求的信息 */
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("id", users.getId() + "");
		ApiResponse<AUsers> apiResponse = this.usersOperService.usersUpdate(paramsMap);
		users = apiResponse.getDataOneJava() ; 
		model.addAttribute("response", apiResponse.toJSON());
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdateInfo";
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePass";
		}else if("updateEmail".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdateEmail";
		}else if("updatePhoto".equalsIgnoreCase(operType))
		{
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePhoto";
		}
		return "/head/users/usersInfo";
	}
	
	
	/**
	 * 打开用户更新
	 * @return
	 */
	@RequestMapping("/usersApiInfo")
	public String usersApiInfo(HttpServletRequest request,Model model,String id)
	{
		
		ConstatFinalUtil.SYS_LOGGER.info("--usersApiInfo--");
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		ApiResponse<AUsersApis> usersApiResponse = this.usersDbService.findOneUsersApiService(condMap);
		/* 改信息已查看过 */
		AUsersApis usersApi = usersApiResponse.getDataOneJava();
		if(usersApi.getIsCheck()==1) {
			model.addAttribute("isCheck", "isCheck");
		}else {
			model.addAttribute("isCheck", "");
			usersApi.setIsCheck(Byte.valueOf("1"));
			usersApi.setUpdateTime(new Date());
			this.usersDbService.updateOneUsersApiService(usersApi);
		}
		usersApiResponse.setInfo("", Collections.EMPTY_MAP);
		model.addAttribute("response", usersApiResponse.toJSON());
		return "/head/users/usersApiInfo";
	}
	
	/**
	 * 打开用户Api列表
	 * @return
	 */
	@RequestMapping("/usersApiList")
	public String usersApiList(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersApiList--");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 从session中获取信息 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 准备请求的信息 */
		Map<String, Object> paramsMap = this.proccedSearch(request);
		paramsMap.put("usersId", users.getId() + "");
		ApiResponse<AUsersApis> apiResponse = this.usersDbService.findCondListUsersApiService(pageInfoUtil, paramsMap);
		apiResponse.setInfo("", Collections.EMPTY_MAP);
		model.addAttribute("response", apiResponse.toJSON());
		return "/head/users/usersApiList";
	}
	
	/**
	 * 打开用户Api列表
	 * @return
	 */
	@RequestMapping("/usersApiInsert")
	public String usersApiInsert(HttpServletRequest request,Model model)
	{
		
		ConstatFinalUtil.SYS_LOGGER.info("--usersApiInsert--");
		/* 准备请求的信息 */
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 从session中获取信息 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 准备请求的信息 */
		AUsersApis apis = new AUsersApis();
		apis.setPubKey(UUID.randomUUID().toString());
		apis.setPriKey(UUID.randomUUID().toString());
		apis.setUsersId(users.getId());
		apis.setUpdateTime(new Date());
		apis.setCreateTime(new Date());
		apis.setPubTime(new Date());
		apis.setStatus(Byte.valueOf("1"));
		apis.setIsCheck(Byte.valueOf("0"));
		
		ApiResponse<Object> apiResponseApi = this.usersDbService.saveOneUsersApiService(apis);
		paramsMap.put("usersId", users.getId() + "");
		ApiResponse<AUsersApis> apiResponse = this.usersDbService.findCondListUsersApiService(pageInfoUtil, paramsMap);
		apiResponse.setInfo(apiResponseApi.getInfo(), Collections.EMPTY_MAP);
		model.addAttribute("response", apiResponse.toJSON());
		return "/head/users/usersApiList";
	}
	
	/**
	 * 打开用户提交
	 * @return
	 */
	@RequestMapping("/usersUpdateSubmit")
	public String usersUpdateSubmit(HttpServletRequest request,Model model)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--usersUpdateSubmit--");
		String operType = request.getParameter("operType");
		/* 从session中获取信息 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		
		/* 准备请求的信息 */
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("id", users.getId() + "");
		paramsMap.put("operType", operType);
		
		if("updateInfo".equalsIgnoreCase(operType))
		{
			/* 用户填写的信息 */
			String nickName = request.getParameter("nickName");
			String trueName = request.getParameter("trueName");
			String birthdayStr = request.getParameter("birthdayStr");
			String address = request.getParameter("address");
			String qq = request.getParameter("qq");
			String phone = request.getParameter("phone");
			String regionId = request.getParameter("regionId");
			String sex = request.getParameter("sex");
			String weiXin = request.getParameter("weiXin");
			String education = request.getParameter("education");
			
			paramsMap.put("nickName", nickName);
			paramsMap.put("trueName", trueName);
			paramsMap.put("birthdayStr", birthdayStr);
			paramsMap.put("address", address);
			paramsMap.put("qq", qq);
			paramsMap.put("phone", phone);
			paramsMap.put("regionId", regionId);
			paramsMap.put("sex", sex);
			paramsMap.put("weiXin", weiXin);
			paramsMap.put("education", education);
			
			ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
			model.addAttribute("response", apiResponse.toJSON());
			/* 根据不同的请求跳转不同的页面 */
			request.getSession().setAttribute("users", users);
			return "/head/users/usersUpdateInfo";
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			String souPass = request.getParameter("souPass");
			String password = request.getParameter("password");
			
			paramsMap.put("souPass", souPass);
			paramsMap.put("password", password);
			
			ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
			model.addAttribute("response", apiResponse.toJSON());
			request.getSession().setAttribute("users", users);
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePass";
		}else if("updateEmail".equalsIgnoreCase(operType))
		{
			String email = request.getParameter("email");
			
			paramsMap.put("email", email);
			
			ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
			model.addAttribute("response", apiResponse.toJSON());
			request.getSession().setAttribute("users", users);
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdateEmail";
		}else if("updatePhoto".equalsIgnoreCase(operType))
		{
			/* 上传头像 */
			try
			{
				/* 更新标志 */
				if(request instanceof MultipartRequest)
				{ 
					MultipartRequest multipartRequest = (MultipartRequest) request ;
					MultipartFile multipartFile = multipartRequest.getFile("file");
					paramsMap.put("fileName", multipartFile.getOriginalFilename());
					paramsMap.put("inputStream", multipartFile.getInputStream());
					users.setUpdateTime(new Date());
					ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
					model.addAttribute("response", apiResponse.toJSON());
					request.getSession().setAttribute("users", users);
				}
			} catch (Exception e)
			{
				ConstatFinalUtil.SYS_LOGGER.error("更新图片失败了;" , e );
			}
			/* 根据不同的请求跳转不同的页面 */
			return "/head/users/usersUpdatePhoto";
		}else
		{
			ApiResponse<Object> apiResponse = this.usersOperService.operUsersUpdateSubmit(paramsMap);
			model.addAttribute("response", apiResponse.toJSON());
		}
		return "/head/users/usersInfo";
	}
	
	/**
	 * 退出页面
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request, HttpSession session)
	{
		ConstatFinalUtil.SYS_LOGGER.info("--logout--");
		String returnUrl = request.getParameter("returnUrl");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 获取用户 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		if(returnUrl == null)
		{
			returnUrl = "" ; 
		}
		try
		{
			returnUrl = URLEncoder.encode(returnUrl, "UTF-8");
			/* 做一些清除的操作 */
			session.removeAttribute("users");
			session.removeAttribute("lastLoginTime");
			
			apiResponse.setCode(ApiResponseEnum.INFO_LOGOUT.getStatus()); 
		} catch (Exception e)
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus()); 
			ConstatFinalUtil.SYS_LOGGER.error("退出报错了;email:{}", users.getEmail(),e);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		/* 提示信息 */
		session.setAttribute("response", apiResponse.toJSON());
		/* 客户端跳转到登陆页面 */
		return "redirect:/login.htm?returnUrl=" + returnUrl;
	}
}
